package entity;

import java.util.LinkedList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import AI.Path;

import constans.Globals;
import constans.ResourceManager;
import controller.ObjectController;
import engine.Map;
import engine.MapMatrix;

public class Unit extends Entity{

	public static float dLife = 0;
	public static float dPrice = 0;
	int speed = 1;
	int maxLife = 10 + (int) (dLife);
	int life = maxLife;
	int movingTime = 0;
	int dir = 0;
	int imgX = 0;
	int price = 5 + (int)dPrice;
	
	boolean findPath = false;
	boolean isDead = false;
	
	LinkedList<Vector2f> path = new LinkedList<Vector2f>();
	
	int t = 0;
	
	public Unit(int x, int y)
	{
		super(x, y);
		img = ResourceManager.getImage("tank");
		type = Entity.UNIT;
		setPath();
		dLife += 0.6;
		dPrice += 0.03;
		
	}
	public Unit(Vector2f pos)
	{
		super((int)pos.x, (int)pos.y);
		img = ResourceManager.getImage("tank");
		type = Entity.UNIT;
		setPath();
		dLife += 0.6;
		dPrice += 0.03;
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		if(picked)
		{
			g.setColor(Color.red);
			g.drawOval(Map.dx + x, y, size, size);
		}
		img.draw(Map.dx + x, y, Map.dx + x + size, y + size, imgX, 0, imgX + size, size);
	}
	public void update(GameContainer container, int delta) throws SlickException
	{
		if(t++ % 2 == 0)
		move();
	}
	public void moveTo(int dir)
	{
		ObjectController.getMap().releaseField(this);
		switch(dir)
		{
		case 1:
			i--;
			j--;
			imgX = 0;
			//img.setRotation(90);
			break;
		case 2:
			j--;
			imgX = 30;
			//img.setRotation(105);
			break;
		case 3:
			i++;
			j--;
			imgX = 60;
			//img.setRotation(130);
			break;
		case 4:
			i--;
			imgX = 90;
			break;
		case 6:
			i++;
			imgX = 120;
			break;
		case 7:
			i--;
			j++;
			imgX = 150;
			break;
		case 8:
			j++;
			imgX = 180;
			break;
		case 9:
			i++;
			j++;
			imgX = 210;
			break;		
		}
		ObjectController.getMap().addEntity(this);
	}
	public void animation(int dir)
	{
		
		switch(dir)
		{
		case 1:
			x-=speed;
			y-=speed;
			break;
		case 2:
			y-=speed;
			break;
		case 3:
			x+=speed;
			y-=speed;
			break;
		case 4:
			x-=speed;
			break;
		case 6:
			x+=speed;
			break;
		case 7:
			x-=speed;
			y+=speed;
			break;
		case 8:
			y+=speed;
			break;
		case 9:
			x+=speed;
			y+=speed;
			break;		
		}
		
	}
	public void move()
	{	
		if(path.isEmpty())
		{
			isDead = true;
			return;
		}
		if(movingTime % (Globals.FIELD_SIZE / speed) == 0)
		{
			if(findPath)
			{
				setPath();
				
				findPath = false;
			}
				
			Vector2f target = path.getFirst();
			//kolizja
			if(!ObjectController.getMap().isFieldEmpty((int)target.x,(int) target.y))
			{
				dir = 0;
				return;
			}
			path.removeFirst();
			
			if(target.x < i && target.y < j) dir = 1;
			else if(target.x == i && target.y < j) dir = 2;
			else if(target.x > i && target.y < j) dir = 3;
			else if(target.x < i && target.y == j) dir = 4;
			else if(target.x > i && target.y == j) dir = 6;
			else if(target.x < i && target.y > j) dir = 7;
			else if(target.x == i && target.y > j) dir = 8;
			else if(target.x > i && target.y > j) dir = 9;
				
			moveTo(dir);
			movingTime = 0;
		}
		animation(dir);
		movingTime++;
	}
	private void setPath()
	{
		Path p = new Path(new Vector2f(i,j), ObjectController.getTarget());
		path = p.getPath();		
	}
	public void findPath()
	{
		findPath = true;
	}
	public boolean isPath()
	{
		Path p = new Path(new Vector2f(i,j), ObjectController.getTarget());
		return p.isPath();
	}
	public boolean isDead()
	{
		return isDead;
	}
	public void increaseLife(int l)
	{
		life -= l;
		if(life <= 0)
			isDead = true;
	}
	public int getPrice()
	{
		return price;
	}
	public int getLife()
	{
		return life;
	}
	public int getMaxLife()
	{
		return maxLife;
	}
	public boolean isOn(int mx, int my) {
		if(mx > x && mx < x+size)
			if(my > y && my < y+size)
			{
				picked = true;
				return true;
			}
		return false;
	}

}
